package cn.jingzhuan.element.demo.view.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.content.ContextCompat
import android.support.v4.content.res.ResourcesCompat
import android.support.v4.view.AsyncLayoutInflater
import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import cn.jingzhuan.element.demo.view.BaseBindingActivity
import cn.jingzhuan.element.demo.view.fragment.LayoutFragment
import cn.jingzhuan.element.demo.view.fragment.UtilFragment
import cn.jingzhuan.element.demo.view.fragment.WidgetFragment
import cn.jingzhuan.element.demo.view.pojo.Item
import cn.jingzhuan.element.demo.view.util.StateListDrawableBuilder
import cn.jingzhuan.element.element.R
import cn.jingzhuan.element.element.databinding.ActivityMainBinding

class MainActivity : BaseBindingActivity<ActivityMainBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_main
    private val fragmentList = ArrayList<Fragment>()
    private val layoutFragment = LayoutFragment() // 默认初始化首页的Fragment
    private val layoutInflater: AsyncLayoutInflater  = AsyncLayoutInflater(this)

    @SuppressLint("InflateParams")
    override fun onDataBind(savedInstanceState: Bundle?, binding: ActivityMainBinding) {
        fragmentList.clear()
        fragmentList.add(layoutFragment)
        fragmentList.add(WidgetFragment())
        fragmentList.add(UtilFragment())
        binding.mainViewPager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {

            override fun getItem(position: Int): Fragment {
                return fragmentList.get(position)
            }

            override fun getCount(): Int {
                return fragmentList.size
            }
        }
        StateListDrawableBuilder.state_pressed

        var items  = ArrayList<Item>()
        items.add(Item(R.string.layout,StateListDrawableBuilder(this)
                .addState(R.mipmap.icon_tabbar_component,-StateListDrawableBuilder.state_selected)
                .addState(R.mipmap.icon_tabbar_component_selected,StateListDrawableBuilder.state_selected)
                .build()))
        items.add(Item(R.string.widget,StateListDrawableBuilder(this)
                .addState(R.mipmap.icon_tabbar_lab, -StateListDrawableBuilder.state_selected)
                .addState(R.mipmap.icon_tabbar_lab_selected, StateListDrawableBuilder.state_selected)
                .build()))
        items.add(Item(R.string.util,StateListDrawableBuilder(this)
                .addState(R.mipmap.icon_tabbar_util, -StateListDrawableBuilder.state_selected)
                .addState(R.mipmap.icon_tabbar_util_selected, StateListDrawableBuilder.state_selected)
                .build()))

        items.forEach {
            layoutInflater.inflate(R.layout.item_main_tab,null,fun (view: View, _: Int, _: ViewGroup?) {
                var icon = view.findViewById<ImageView>(R.id.tab_icon)
                var text = view.findViewById<TextView>(R.id.tab_text)
                icon.setImageDrawable(it.icon)
                text.setText(it.text)
                text.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_text_selected))
                var tab = binding.tabLayout.newTab()
                tab.customView = view
                binding.tabLayout.addTab(tab)
            })
        }

        binding.mainViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                val tab = binding.tabLayout.getTabAt(position)
                if (tab != null) tab!!.select()
            }

        })
        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val position = binding.tabLayout.selectedTabPosition
                if (binding.mainViewPager != null) binding.mainViewPager.setCurrentItem(position, false)
            }

        })
    }


}